package com.qingzhu.controller.addressserver;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qingzhu.pojo.ReceivingAddress;
import com.qingzhu.pojovo.ResultVo;
import com.qingzhu.service.ReceivingAddressService;
import com.qingzhu.service.impl.ReceivingAddressServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/receiving/address/add")
public class ReceivingAddressInsertController extends HttpServlet {
    ObjectMapper objectMapper = new ObjectMapper();
    ReceivingAddressService receivingAddressService = new ReceivingAddressServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("添加收货地址服务..");
        String regionJson = req.getReader().readLine();
        ReceivingAddress receivingAddress = objectMapper.readValue(regionJson, ReceivingAddress.class);
        int result = receivingAddressService.addOneReceivingAddress(receivingAddress);
        ResultVo resultVo = result > 0 ?
                new ResultVo(200,"添加成功！"):new ResultVo(500,"添加失败！");
        resp.getWriter().write(objectMapper.writeValueAsString(resultVo));
    }
}
